package com.zoke.support.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.zoke.support.R;
import com.zoke.support.view.ZokeButton;
import com.zoke.support.view.ZokeTextView;

/**
 * 弹出窗口 (需要在子类实现布局并需要设置指定的id）
 * 
 * @author JackWu
 * @version 1.0
 * @date 2014-10-9
 * @_QQ_ 651319154
 */
public abstract class BaseDialog extends Dialog implements View.OnClickListener {

	protected ZokeTextView mTitleTv;
	protected ZokeButton mLeftBtn;
	protected ZokeButton mRightBtn;
	/** 是否可以取消 默认为true **/
	protected boolean isCancelable = true;
	/** 是否点击外面可以取消弹出框 默认为true **/
	protected boolean isCanceledOnOutside = true;
	protected LinearLayout mSubContainer;

	protected LayoutInflater mInflater;

	private LeftClickLinstener mLeftLinstener;
	private RightClickLinstener mRightLinstener;

	public BaseDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public BaseDialog(Context context, int theme) {
		super(context, R.style.basedialog);
	}

	public BaseDialog(Context context) {
		super(context, R.style.basedialog);
		mInflater = LayoutInflater.from(context);
		setContentView(getLayoutRes());
		// dialog的标题
		mTitleTv = (ZokeTextView) findViewById(R.id._basedialog_title);
		// 左侧按钮
		mLeftBtn = (ZokeButton) findViewById(R.id._basedialog_leftbutton);
		// 右侧按钮
		mRightBtn = (ZokeButton) findViewById(R.id._basedialog_rightbutton);
		// 子控件的容器
		mSubContainer = (LinearLayout) findViewById(R.id._basedialog_subcontainer);
		mLeftBtn.setOnClickListener(this);
		mRightBtn.setOnClickListener(this);
		setCancelable(isCancelable);
		setCanceledOnTouchOutside(isCanceledOnOutside);
	}

	public BaseDialog setCancelabled(boolean isCancel) {
		setCancelable(isCancel);
		return this;
	}

	public BaseDialog setCancelOutside(boolean isCacel) {
		setCanceledOnTouchOutside(isCacel);
		return this;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	/** dialog的布局 **/
	protected abstract int getLayoutRes();

	/** 设置标题 **/
	public BaseDialog setDialogTitle(CharSequence text) {
		if (null != mTitleTv)
			mTitleTv.setText(text);
		return this;
	}

	/** 设置左侧按钮文本 **/
	public BaseDialog setLeftText(CharSequence text) {
		if (null != mLeftBtn)
			mLeftBtn.setText(text);
		return this;
	}

	/** 设置左侧点击事件 **/
	public BaseDialog setLeftClick(LeftClickLinstener listener) {
		this.mLeftLinstener = listener;
		return this;
	}

	/** 设置右侧按钮文本 **/
	public BaseDialog setRightText(CharSequence text) {
		if (null != mRightBtn)
			mRightBtn.setText(text);
		return this;
	}

	/** 设置右侧按钮点击监听 **/
	public BaseDialog setRightClick(RightClickLinstener listener) {
		this.mRightLinstener = listener;
		return this;
	}

	/** 设置副标题或者是其他控件 **/
	public BaseDialog setSubContent(View view) {
		if (null != mSubContainer) {
			if (mSubContainer.getChildCount() > 0) {
				mSubContainer.removeAllViews();
			}
			mSubContainer.setVisibility(View.VISIBLE);
			RelativeLayout result = new RelativeLayout(getContext());
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
					android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
			params.addRule(RelativeLayout.CENTER_IN_PARENT);
			result.addView(view, params);
			// 添加控件
			mSubContainer.addView(result, new LinearLayout.LayoutParams(
					android.view.ViewGroup.LayoutParams.MATCH_PARENT,
					android.view.ViewGroup.LayoutParams.MATCH_PARENT, 1));
		}
		return this;
	}

	public BaseDialog hideSubContent(View view) {
		if (null != mSubContainer)
			mSubContainer.setVisibility(View.GONE);
		return this;
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id._basedialog_leftbutton) {
			onClickLeft(view);
			return;
		}

		if (id == R.id._basedialog_rightbutton) {
			onClickRight(view);
			return;
		}
	}

	/** 左侧按钮点击事件 **/
	private void onClickLeft(View view) {
		if (null != mLeftLinstener)
			mLeftLinstener.onClickLeft(view);
	}

	/** 右侧按钮点击 **/
	private void onClickRight(View view) {
		if (null != mRightLinstener)
			mRightLinstener.onClickRight(view);
	}

	/** 左侧按钮点击监听 **/
	public interface LeftClickLinstener {
		public void onClickLeft(View view);
	}

	/** 右侧按钮点击监听 **/
	public interface RightClickLinstener {
		public void onClickRight(View view);
	}

}
